//
//  XFDLogInForgetView.m
//  XFDLogin_Example
//
//  Created by 韩冰 on 2017/11/3.
//  Copyright © 2017年 GL-ZHU. All rights reserved.
//

#import "XFDLogInForgetView.h"
#import "XFDLogInInputView.h"
#import <XFDCommon/HBCommon.h>
#import <Masonry/Masonry.h>
#import <BlocksKit/UIControl+BlocksKit.h>
#import <XFDCommon/NSDictionary+HBSafely.h>
#import <XFDCommon/XFDUserManage.h>
#import <XFDCommon/FLToast+UIView.h>

@interface XFDLogInForgetView()
@property (nonatomic, strong)XFDLogInInputView *phoneView;
@property (nonatomic, strong)XFDLogInInputView *codeView;
@property (nonatomic, strong)XFDLogInInputView *passWordView;
@property (nonatomic, strong)UIButton *sureButton;
@end
@implementation XFDLogInForgetView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
        [self handleInputBlock];
    }
    return self;
}
- (void)codeButtonAction{
    [self.codeView openCountdown];
}

- (void)createSubViews{
    [self addSubview:self.phoneView];
    [self addSubview:self.codeView];
    [self addSubview:self.passWordView];
    [self addSubview:self.sureButton];
    [self.phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).priorityHigh();
        make.right.equalTo(self.mas_right).priorityHigh();
        make.left.equalTo(self.mas_left).priorityHigh();
        make.height.mas_offset(50).priorityHigh();
    }];
    [self.codeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.phoneView. mas_bottom).priorityHigh();
        make.right.equalTo(self.mas_right).priorityHigh();
        make.left.equalTo(self.mas_left).priorityHigh();
        make.height.mas_offset(50).priorityHigh();
    }];
    [self.passWordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.codeView. mas_bottom).priorityHigh();
        make.right.equalTo(self.mas_right).priorityHigh();
        make.left.equalTo(self.mas_left).priorityHigh();
        make.height.mas_offset(50).priorityHigh();
    }];
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passWordView. mas_bottom).with.offset(50).priorityHigh();
        make.right.equalTo(self.mas_right).with.offset(-40).priorityHigh();
        make.left.equalTo(self.mas_left).with.offset(40).priorityHigh();
        make.height.mas_offset(40).priorityHigh();
    }];
    NSString *str = [XFDUserManage accountPhone];
    if (str.length >0) {
        self.phoneView.defultText = str;
    }
    
}

- (void)handleInputBlock{
    WEAKSELF
    self.phoneView.getTextFromeInputView = ^(NSString *str) {
        STRONGSELF
        [strongSelf handleSureButton];
    };
    self.codeView.getTextFromeInputView = ^(NSString *str) {
        STRONGSELF
        [strongSelf handleSureButton];
    };
    self.codeView.getCodeBlock = ^{
        STRONGSELF
        if (strongSelf.forgetGetCodeBlock) {
            if (strongSelf.phoneView.inputText.length != 11) {
                [THEAPP.window makeToast:@"请输入正确的手机号"];
            }else{
                strongSelf.forgetGetCodeBlock(strongSelf.phoneView.inputText);
            }
        }
    };
    self.passWordView.getTextFromeInputView = ^(NSString *str) {
        STRONGSELF
        [strongSelf handleSureButton];
    };
    
}
- (void)handleSureButton{
    
    if (self.phoneView.inputText.length > 0 && self.codeView.inputText.length > 0&& self.passWordView.inputText.length >0) {
        self.sureButton.backgroundColor = GlobalBuleColor;
        self.sureButton.userInteractionEnabled = YES;
    }else{
        self.sureButton.backgroundColor = GlobalGrayOneColor;
        self.sureButton.userInteractionEnabled = NO;
    }
}

- (XFDLogInInputView *)phoneView{
    if (!_phoneView) {
        _phoneView = [[XFDLogInInputView alloc]initWithFrame:CGRectZero withIsCodeButton:NO];
        _phoneView.placeholder = @"请输入用户名或手机号";
        _phoneView.isClear = YES;
    }
    return _phoneView;
}

- (XFDLogInInputView *)codeView{
    if (!_codeView) {
        _codeView = [[XFDLogInInputView alloc]initWithFrame:CGRectZero withIsCodeButton:YES];
        _codeView.codeBtnTitle = @"获取验证码";
        _codeView.placeholder = @"请输入验证码";
        _codeView.isCiphertext = YES;
    }
    return _codeView;
}
- (XFDLogInInputView *)passWordView{
    if (!_passWordView) {
        _passWordView = [[XFDLogInInputView alloc]initWithFrame:CGRectZero withIsCodeButton:NO];
        _passWordView.placeholder = @"请输入新密码";
        _passWordView.isCiphertButton = YES;
    }
    return _passWordView;
}
- (UIButton *)sureButton{
    if (!_sureButton) {
        _sureButton =  [UIButton buttonWithType:UIButtonTypeCustom];
        [_sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_sureButton setTitle:@"确认" forState:UIControlStateNormal];
        _sureButton.backgroundColor = GlobalGrayOneColor;
        _sureButton.titleLabel.font = [UIFont systemFontOfSize:14];
        _sureButton.layer.cornerRadius = 5;
        _sureButton.layer.masksToBounds = YES;
        _sureButton.userInteractionEnabled = NO;
        WEAKSELF
        [_sureButton  bk_addEventHandler:^(id sender) {
            
            STRONGSELF
            if (strongSelf.phoneView.inputText.length != 11) {
                [THEAPP.window makeToast:@"请输入正确的手机号"];
                return ;
            }
            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
            [dict setObjectSafely:strongSelf.phoneView.inputText forKey:@"mobilephone"];
            [dict setObjectSafely:strongSelf.codeView.inputText forKey:@"smsCode"];
            [dict setObjectSafely:strongSelf.passWordView.inputText forKey:@"password"];
            if (strongSelf.forgetBlock) {
                strongSelf.forgetBlock(dict);
            }
        } forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureButton;
}
@end
